<?php    
/**
 * Custom controller for news feeds.
 */
class ControllerCatalogAttribute extends Controller { 
	private $error = array();
  
	/**
	 * For indexing operations.
	 */
  	public function index() {
		// Load languages
		$this->load->language('catalog/attribute');
		
		$this->document->title = $this->language->get('heading_title');
		 
		// Load the model
		$this->load->model('catalog/attribute');
		
    	$this->getList();
  	}
  
	/**
	 * For attribute insert operations.
	 */
  	public function insert() {
		// Load language
		$this->load->language('catalog/attribute');

    	$this->document->title = $this->language->get('heading_title');
		
		// Load model
		$this->load->model('catalog/attribute');
			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_attribute->addAttribute($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url);
		}
    
    	$this->getForm();
  	} 
   
	/**
	 * For attribute update operations.
	 */
  	public function update() {
		$this->load->language('catalog/attribute');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_attribute->editAttribute($this->request->get['attribute_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url);
		}
    
    	$this->getForm();
  	}   

	/**
	 * For attribute delete operations.
	 */
  	public function delete() {
		$this->load->language('catalog/attribute');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/attribute');
			
    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $attribute_id) {
				$this->model_catalog_attribute->deleteAttribute($attribute_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url);
    	}
	
    	$this->getList();
  	}  
    
  	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/insert' . $url;
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/delete' . $url;	

		$this->data['attribute'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		// Get the total number of news feeds available
		$attributes_total = $this->model_catalog_attribute->getTotalAttributes();
	
		$results = $this->model_catalog_attribute->getAllAttributes($data);
 
    	foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=catalog/attribute/update&attribute_id=' . $result['attribute_id'] . $url
			);
						
			$this->data['attributes'][] = array(
				'attribute_id' 	 	=> $result['attribute_id'],
				'name'  			=> $result['name'],
				'data_type'     	=> $result['data_type'],
				'default_value'	 	=> $result['default_value'],
				'description'	 	=> $result['description'],
				'selected'   		=> isset($this->request->post['selected']) && in_array($result['attribute_id'], $this->request->post['selected']),
				'action'     		=> $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		
		$this->data['text_published'] = $this->language->get('text_published');
		$this->data['text_not_published'] = $this->language->get('text_not_published');
		$this->data['text_characters_left'] = $this->language->get('text_characters_left');
		
		// Column labels
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_data_type'] = $this->language->get('column_data_type');					
		$this->data['column_description'] = $this->language->get('column_description');	
		
		$this->data['column_action'] = $this->language->get('column_action');	
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=' .  'DESC';
		} else {
			$url .= '&order=' .  'ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&sort=name' . $url;
		$this->data['sort_sort_order'] = HTTPS_SERVER . 'index.php?route=catalog/attribute&sort=sort_order' . $url;
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $attributes_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		
		$this->template = 'catalog/attribute_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
  
	/**
	 * Create form for inputs.
	 */
  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_default'] = $this->language->get('text_default');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_data_type'] = $this->language->get('entry_data_type');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_default_value'] = $this->language->get('entry_default_value');
  
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
	  
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		// If attribute name has not been set
		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}	

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/attribute',
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		    
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
							
		if (!isset($this->request->get['attribute_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/insert' . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/attribute/update&attribute_id=' . $this->request->get['attribute_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=catalog/attribute' . $url;

		// Edits
    	if (isset($this->request->get['attribute_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$attribute_info = $this->model_catalog_attribute->getAttribute($this->request->get['attribute_id']);
    	}
					
		
		// Attribute name
    	if (isset($this->request->post['name'])) {
      		$this->data['name'] = $this->request->post['name'];
    	} elseif (isset($attribute_info)) {
			$this->data['name'] = $attribute_info['name'];
		} else {	
      		$this->data['name'] = '';
    	}
		
		// Attribute default value
		if (isset($this->request->post['default_value'])) {
			$this->data['default_value'] = $this->request->post['default_value'];
		} elseif (isset($attribute_info)) {
			$this->data['default_value'] = $attribute_info['default_value'];
		} else {
			$this->data['default_value'] = '';
		}
		
		// Attribute data type
		if (isset($this->request->post['data_type'])) {
			$this->data['data_type'] = $this->request->post['data_type'];
		} elseif (isset($attribute_info)) {
			$this->data['data_type'] = $attribute_info['data_type'];
		} else {
			$this->data['data_type'] = '';
		}
		
		// Description
		if (isset($this->request->post['description'])) {
			$this->data['description'] = $this->request->post['description'];
		} elseif (isset($attribute_info)) {
			$this->data['description'] = $attribute_info['description'];
		} else {
			$this->data['description'] = '';
		}
				
		$this->template = 'catalog/attribute_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}  
	 
	 /**
	  * Performs validation on form data
	  */ 
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'catalog/attribute')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

		if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 50)) {
      		$this->error['name'] = $this->language->get('error_required_data');
    	}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}    

	/**
	 * Validate if user can delete attributes.
	 */
  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'catalog/attribute')) {
			$this->error['warning'] = $this->language->get('error_permission');
    	}	
		
		// Validate if we can really delete the attributes
		$this->load->model('catalog/category');		
		
		$total_assigned = 0;
		foreach ($this->request->post['selected'] as $attribute_id) {
  			$category_total = $this->model_catalog_category->getTotalCategoriesByAttributeId($attribute_id);
    
			if ($category_total) {
				$total_assigned += $category_total;
	  			
			}	
	  	}
		
		if ($total_assigned > 0) {
			$this->error['warning'] = sprintf($this->language->get('error_category'), $total_assigned);	
		}
	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}  
  	}
}
?>